package com.itbaizhan.shopping_order_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itbaizhan.shopping_common.pojo.Address;
import com.itbaizhan.shopping_common.pojo.Area;
import com.itbaizhan.shopping_common.pojo.City;
import com.itbaizhan.shopping_common.pojo.Province;
import com.itbaizhan.shopping_common.service.AddressService;
import com.itbaizhan.shopping_order_service.mapper.AddressMapper;
import com.itbaizhan.shopping_order_service.mapper.AreaMapper;
import com.itbaizhan.shopping_order_service.mapper.CityMapper;
import com.itbaizhan.shopping_order_service.mapper.ProvinceMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地址服务实现类
 */
@DubboService
@Service
public class AddressServiceImpl implements AddressService {
	
	
	@Autowired
	private AddressMapper addressMapper;//地址
	
	@Autowired
	private ProvinceMapper provinceMapper;//省
	
	@Autowired
	private CityMapper cityMapper;//城市
	
	@Autowired
	private AreaMapper areaMapper;//区县
	
	/**
	 * 查询所有省份
	 *
	 * @return
	 */
	public List<Province> findAllProvince() {
		return provinceMapper.selectList(null);
	}
	
	/**
	 * 根据省份查询城市
	 *
	 * @param provinceId
	 * @return
	 */
	public List<City> findCityByProvince(Long provinceId) {
		QueryWrapper<City> wrapper = new QueryWrapper<>();
		wrapper.eq("provinceId", provinceId);
		return cityMapper.selectList(wrapper);
	}
	
	/**
	 * 根据城市查询区域
	 *
	 * @param cityId
	 * @return
	 */
	public List<Area> findAreaByCity(Long cityId) {
		QueryWrapper<Area> wrapper = new QueryWrapper<>();
		wrapper.eq("cityId", cityId);
		return areaMapper.selectList(wrapper);
	}
	
	
	/**
	 * 添加地址
	 *
	 * @param address
	 */
	public void add(Address address) {
		addressMapper.insert(address);
	}
	
	/**
	 * 修改地址
	 *
	 * @param address
	 */
	public void update(Address address) {
		addressMapper.updateById(address);
	}
	
	/**
	 * 根据id查询地址
	 *
	 * @param id
	 * @return
	 */
	public Address findById(Long id) {
		return addressMapper.selectById(id);
	}
	
	/**
	 * 删除地址
	 *
	 * @param id
	 */
	public void delete(Long id) {
		addressMapper.deleteById(id);
	}
	
	/**
	 * 根据用户查询地址
	 *
	 * @param userId
	 * @return
	 */
	public List<Address> findByUser(Long userId) {
		QueryWrapper<Address> queryWrapper = new QueryWrapper();
		queryWrapper.eq("userId",userId);
		List<Address> addresses = addressMapper.selectList(queryWrapper);
		return addresses;
	}
}
